package com.kaikeba.dao.impl;

import com.kaikeba.bean.Student;
import com.kaikeba.dao.StudentDao;
import com.kaikeba.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author: Gyx
 * @Date: 2021/2/18 23:04
 */
public class StudentDaoImpl implements StudentDao {
    private DruidUtil ds = new DruidUtil();
    private List<Student> list = new ArrayList<>();
    @Override
    public boolean insertStu(Student student) {
        Connection conn = ds.getConnection();
        PreparedStatement stat = null;
        String sql = "insert into project6_student ( studentNumber,studentName,studentClass,studentAge)values (?,?,?,?)";
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1,student.getStudentNumber());
            stat.setString(2,student.getStudentName());
            stat.setString(3,student.getStudentClass());
            stat.setInt(4,student.getStudentAge());
            int i = stat.executeUpdate();
            if(i!=0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ds.close(conn,stat,null);
        }
        return false;
    }

    @Override
    public boolean updateStu(Student student) {
        Connection conn = ds.getConnection();
        PreparedStatement stat = null;
        String sql = "update project6_student set studentNumber=?,studentName=?,studentClass=?,studentAge=? where studentId=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1,student.getStudentNumber());
            stat.setString(2,student.getStudentName());
            stat.setString(3,student.getStudentClass());
            stat.setInt(4,student.getStudentAge());
            stat.setInt(5,student.getStudentId());
            int i = stat.executeUpdate();
            if(i!=0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ds.close(conn,stat,null);
        }
        return false;
    }

    @Override
    public boolean deleteStu(int studentId) {
        Connection conn = ds.getConnection();
        PreparedStatement stat = null;
        String sql = "delete from project6_student where studentId=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1,studentId);
            int i = stat.executeUpdate();
            if(i!=0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ds.close(conn,stat,null);
        }
        return false;
    }

    @Override
    public Student selectStu(int studentId) {
        Connection conn = ds.getConnection();
        PreparedStatement stat = null;
        ResultSet rs = null;
        Student student = new Student();
        String sql = "select * from project6_student where studentId=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1,studentId);
            rs = stat.executeQuery();
            if(rs.next()){
                student.setStudentId(studentId);
                student.setStudentNumber(rs.getString("studentNumber"));
                student.setStudentName(rs.getString("studentName"));
                student.setStudentClass(rs.getString("studentClass"));
                student.setStudentAge(rs.getInt("studentAge"));
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ds.close(conn,stat,rs);
        }
        return null;
    }

    @Override
    public List<Student> selectAll() {
        Connection conn = ds.getConnection();
        PreparedStatement stat = null;
        ResultSet rs = null;
        Student student = new Student();
        String sql = "select * from project6_student";
        try {
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery();
            while (rs.next()){
                int studentId = rs.getInt("studentId");
                String studentNumber = rs.getString("studentNumber");
                String studentName = rs.getString("studentName");
                String studentClass = rs.getString("studentClass");
                int studentAge = rs.getInt("studentAge");
                list.add(new Student(studentId,studentNumber,studentName,studentClass,studentAge));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            ds.close(conn,stat,rs);
        }
        return list;
    }
}
